package com.css.fxfzmh.modules.symbolManage.repository.impl;

import com.css.fxfzmh.constants.YNEnum;
import com.css.fxfzmh.modules.symbolManage.entity.Symbol;
import com.css.fxfzmh.modules.symbolManage.param.SymbolParam;
import com.css.fxfzmh.modules.symbolManage.repository.SymbolRepository;
import com.css.fxfzmh.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/05/09
 * @Description:
 */
@Repository
public class SymbolRepositoryImpl implements SymbolRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private static final String TABLE_NAME = "mh_symbol_manage";

    @Override
    public List<Symbol> getPage(SymbolParam param) {
        StringBuilder sql = new StringBuilder("select * from "+TABLE_NAME+" where del_flag ='0' ");
        //时间范围
        if (!PlatformObjectUtils.isEmpty(param.getStartTime()) & !PlatformObjectUtils.isEmpty(param.getEndTime())) {
            sql.append(" and create_time between '" + param.getStartTime() + "' and '" + param.getEndTime() + "'");
        }
        //大于开始时间
        if (!PlatformObjectUtils.isEmpty(param.getStartTime()) & PlatformObjectUtils.isEmpty(param.getEndTime())) {
            sql.append(" and create_time  >='" + param.getStartTime() + "'");
        }
        //小于结束时间
        if ( !PlatformObjectUtils.isEmpty(param.getEndTime()) & PlatformObjectUtils.isEmpty(param.getStartTime())) {
            sql.append(" and create_time  <='" + param.getEndTime() + "'");
        }
        //符号名称
        if (!PlatformObjectUtils.isEmpty(param.getSymbolName())) {
            sql.append(" and symbol_name like '%" + param.getSymbolName() + "%'");
        }
        //符号类型
        if (!PlatformObjectUtils.isEmpty(param.getTypeName())) {
            sql.append(" and type_name = '" + param.getTypeName() + "'");
        }
        //上传人名称
        if (!PlatformObjectUtils.isEmpty(param.getUserName())) {
            sql.append(" and user_name like '%" + param.getUserName() + "%'");
        }
        sql.append(" order by create_time desc");
        List<Symbol> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(Symbol.class));
        return list;
    }

    @Override
    public void save(Symbol symbol) {
        String sql="insert into "+TABLE_NAME+"(id,type_name,attach_id,symbol_name,symbol_desc,create_user,create_time,del_flag,svg_flag,user_name,svg) " +
                "values(:id,:typeName,:attachId,:symbolName,:symbolDesc, :createUser, :createTime,:delFlag,:svgFlag,:userName,:svg)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(symbol);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void delete(List<String> asList) {
        String sql = "update "+TABLE_NAME+" set del_flag = '"+ YNEnum.Y.toString()+"' where id in  ('" + String.join("','", asList) + "')";
        jdbcTemplate.update(sql);
    }
}
